package com.xmcc.controller;


import com.xmcc.common.ResultJson;
import com.xmcc.dto.SysRoleDto;
import com.xmcc.pojo.SysRole;
import com.xmcc.service.SysCoreService;
import com.xmcc.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("sys/role")
public class SysRoleController {
    @Autowired
    private SysRoleService sysRoleService;

    @RequestMapping("role.page")
    public String rolePage(){
        return "role";
    }

    @RequestMapping("list.json")
    @ResponseBody
    public ResultJson roleList(){
        List<SysRole> list = sysRoleService.selectAllRole();
        return ResultJson.success(list);
    }

    @RequestMapping("insert.json")
    @ResponseBody
    public ResultJson insert(SysRoleDto sysRoleDto){
        sysRoleService.insert(sysRoleDto);
        return ResultJson.success("添加成功!!");
    }

    @RequestMapping("update.json")
    @ResponseBody
    public ResultJson update(SysRoleDto sysRoleDto){
        sysRoleService.update(sysRoleDto);
        return ResultJson.success("修改成功!!");
    }

    //修改角色于权限的中间表数据：
    @RequestMapping("changeAcls.json")
    @ResponseBody
    public ResultJson changeAcls(@RequestParam("roleId") Integer roleId,@RequestParam("aclIds") List<Integer> aclIds){
        sysRoleService.changeAcls(roleId,aclIds);
        return ResultJson.success();
    }

    //查询出角色于用户的数据：
    @RequestMapping("users.json")
    @ResponseBody
    public ResultJson usersList(Integer roleId){
        return ResultJson.success(sysRoleService.usersList(roleId));
    }

    //修改角色于用户的中间表数据：
    @RequestMapping("changeUsers.json")
    @ResponseBody
    public ResultJson changeUsers(@RequestParam("roleId") Integer roleId,@RequestParam("userIds") List<Integer> userIds){
        sysRoleService.changeUsers(roleId,userIds);
        return ResultJson.success();
    }



}
